package com.yjd.comm.base.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yjd.comm.util.HttpRequestDeviceUtils;


/**
 * 手机Agent拦截Filter
 * 目的用于拦截手机浏览器转发到微站
 * @author Administrator
 *
 */
public class DeviceFilter implements Filter{

	private String sendRedirectDomain;
	
	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		if(HttpRequestDeviceUtils.isMobileDevice(req)){
			String uri = req.getRequestURI();
			if(uri.startsWith("/spread/regist")){
				String targetURL = new StringBuilder().append(this.sendRedirectDomain).append(uri).append("?").append(req.getQueryString()).toString();
				res.sendRedirect(targetURL);
				return ;
			}
			chain.doFilter(request, response);
		}else{
			chain.doFilter(request, response);
		}
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.sendRedirectDomain = config.getInitParameter("redirectDomain");
	}
 

}
